package com.oceansoft.mobile.biz.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.biz.api.entity.Member;
import com.oceansoft.mobile.biz.api.entity.UnlockCallPolice;
import com.oceansoft.mobile.common.Constant;
import com.oceansoft.mobile.common.util.HttpClientUtil;
import com.oceansoft.mobile.common.util.Result;
import com.oceansoft.mobile.weixin.util.RequestUtils;
import com.oceansoft.mobile.weixin.util.WeixinUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("api/callPolice")
public class ApiCallPoliceController {
	//开锁接口URL
	static final String unlockSubmitUrl = Constant.host5+ "api/CallPolice/SetWXBJB";
	//微信报警接口URL
	static final String callpliceSubmitUrl = Constant.host5+ "api/CallPolice/SetWXBJB2";

	@RequestMapping("getPage/{pageName}")
	public String getPage(@PathVariable("pageName") String pageName, HttpSession session, Model model, HttpServletRequest request) {
		model.addAttribute("openId",(String)session.getAttribute("openId"));
		 String source = (String) request.getSession().getAttribute("source");
	       if (!StringUtils.isEmpty(source)) {
	       	model.addAttribute("source", source);
	       }
		 String location = (String) request.getSession().getAttribute("location");
	       if (!StringUtils.isEmpty(location)) {
	       	model.addAttribute("location", location);
	       }
		return "weixin/callpolice/" + pageName;
	}
	
	@RequestMapping(value = "gotoPageWithUser/{pageName}")
	public String gotoPage(@PathVariable("pageName") String pageName, HttpSession sessiion, HttpServletRequest request,
						   Model model) throws IllegalArgumentException, IOException {
		boolean b = WeixinUtil.checkIsLoginUser(request);
		if(!b){
			model.addAttribute("openId",(String)sessiion.getAttribute("openId"));
			model.addAttribute("gotoPage","/api/callPolice/gotoPageWithUser/"+ pageName);
			return "weixin/login/login";
		}else{
			Member member = (Member)sessiion.getAttribute(Constant.SESSION_USER);
			model.addAttribute("userId",member.getGuid());
			model.addAttribute("openId",member.getOpenId());
			model.addAttribute("userName",member.getRealName());
			model.addAttribute("mobile",member.getMobile());
			 String source = (String) request.getSession().getAttribute("source");
		       if (!StringUtils.isEmpty(source)) {
		       	model.addAttribute("source", source);
		       }
			 String location = (String) request.getSession().getAttribute("location");
		       if (!StringUtils.isEmpty(location)) {
		       	model.addAttribute("location", location);
		       }
			return "weixin/callpolice/" + pageName;
		}

	}

	@ResponseBody
	@RequestMapping(value="unlockSubmit")
	public Result submitUnlock(UnlockCallPolice unlock, HttpServletRequest request) throws IllegalArgumentException, IOException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("WXH", (String)(request.getSession().getAttribute("openId")));
		params.put("LXDH", unlock.getLxdh());
		params.put("SFDZ", unlock.getSfdz()+"("+unlock.getDqdmmc()+")");
		params.put("BJNR", unlock.getBjnr());
		params.put("XM", unlock.getXm());
		params.put("KSZL", unlock.getKszl());
		params.put("CPHM", unlock.getCphm());
		params.put("DQDM", unlock.getDqdm());
		params.put("KSDDZD", unlock.getDwdz());
		params.put("LONGITUDE", unlock.getLongitude());
		params.put("LATITUDE", unlock.getLatitude());
		params.put("BJLX","2");
		String a = HttpClientUtil.doPost(unlockSubmitUrl, params, "utf-8");
		JSONObject json= JSONObject.parseObject(a);
		Result re=new Result(Result.SUCC);
		if((Boolean)json.get("succ")){
//			re.setSucc(Result.SUCC);
			re.setData(json.get("data"));
		}else{
			re.setSucc(Result.FAILURE);
			re.setMsg(json.get("msg").toString());
		}
		return 	re;
	}
	@ResponseBody
	@RequestMapping(value="callPliceSubmit")
	public Result submitCallPlice(HttpServletRequest request) throws IllegalArgumentException, IOException {
		Map<String, String> params = RequestUtils.buildParamsByNamesFromRequest(request);
		params.put("sfdz",params.get("sfdz")+params.get("dqdmmc"));
		params.put("ksddzd", params.get("dwdz"));
		params.put("bjlx", "1");
		params.put("kszl", "00");
		params.put("bjnr", params.get("bjnr")+",（备注：性别:"+params.get("xb")+",是否方便接听来电:"+params.get("sffbjtdh")+"）");
		params.put("xm","xm");
		Result re = JSONObject.parseObject(HttpClientUtil.doPost(callpliceSubmitUrl, params, "utf-8"), Result.class);
		return 	re;
	}

	@ResponseBody
	@RequestMapping(value="fzpbjSubmit")
	public Result fzpbjSubmit(HttpServletRequest request) throws IllegalArgumentException, IOException {
		Map<String, String> params = RequestUtils.buildParamsByNamesFromRequest(request);
		Result re = JSONObject.parseObject(HttpClientUtil.doPost(callpliceSubmitUrl, params, "utf-8"), Result.class);
//		Result re = new Result("提交成功1212");
		return 	re;
	}

}
